﻿using System;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Navigation;
using Autofac;

namespace Phoenix.Extensions.Autofac
{
    public abstract class AutofacPhoenixApplication : PhoenixApplication
    {
        protected override Task<IPhoenixHost> InitialisePhoenix(FrameworkElement content, NavigationEventArgs navigationEventArgs)
        {
            var hostBuilder = new AutofacPhoenixHostBuilder(
                TaskScheduler.FromCurrentSynchronizationContext(),
                Deployment.Current.Dispatcher);
            hostBuilder.RegisterAssembly(GetType().Assembly);
            hostBuilder.ConfigureContainer(ConfigureContainer);
            ConfigurePhoenixHostBuilder(hostBuilder);

            HandleDeepLinks(navigationEventArgs.Uri, hostBuilder);
            return hostBuilder.Run(this);
        }

        // ReSharper disable EventNeverSubscribedTo.Local
        // ReSharper disable UnusedMember.Local
        // ReSharper disable EventNeverInvoked
        new event StartupEventHandler Startup;
        // ReSharper restore EventNeverSubscribedTo.Local
        // ReSharper restore UnusedMember.Local
        // ReSharper restore EventNeverInvoked

        protected abstract void ConfigureContainer(ContainerBuilder containerBuilder);
    }
}